﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SWBetongXaydung.DAL;

namespace SWBetongXaydung.BLL
{
    class ThongKeTongHopThanhToan
    {
        //Số dư: (-) Nợ, (+) Có
        public int IDTaiXe { get; set; }
        public string TenDayDu { get; set; }
        public string SoXe { get; set; }
        public decimal SoDuDauKy { get; set; }
        public decimal TienMat { get; set; }
        public decimal SoDauTieuThu { get; set; }
        public decimal SoDauTamUng { get; set; }
        public decimal SoDauConLai { get { return SoDauTieuThu - SoDauTamUng; } }
        public decimal DonGia { get; set; }
        public decimal ThanhTien { get; set; }
        //public double SoDuCo_ThanhTien { get; set; }
        public decimal GiaTriCuoiKy { get { return SoDuDauKy + ThanhTien + TienMat; } }
        public bool DaQuyetToan { get; set; }
        public int Thang { get; set; }
        public int Nam { get; set; }
        public DateTime NgayQuyetToan { get; set; }
        public static List<ThongKeTongHopThanhToan> GetListByThang(int thang, int nam)
        {
            using (var db = DBContextHelper.GetContext())
            {
                var listQuyetToan = (from e in db.QuyetToanDbSet
                                     where e.Thang == thang &&
                                           e.Nam == nam &&
                                           e.DaQuyetToan == true
                                     orderby e.TaiXe.Ten ascending, e.TaiXe.HoDem ascending
                                     select e).ToList();
                if (listQuyetToan != null && listQuyetToan.Count > 0)
                {
                    List<ThongKeTongHopThanhToan> listThongKe = new List<ThongKeTongHopThanhToan>();
                    foreach (var item in listQuyetToan)
                    {
                        ThongKeTongHopThanhToan thongKeObj = new ThongKeTongHopThanhToan();
                        thongKeObj.IDTaiXe = item.IDTaiXe;
                        thongKeObj.TenDayDu = item.TaiXe.TenDayDu;
                        thongKeObj.SoXe = item.SoXe;
                        thongKeObj.SoDuDauKy = item.SoDuDauKy;
                        thongKeObj.TienMat = item.ThuTienMat;

                        var listCapNhienLieu = ThongKeCapNhienLieu.GetList(thang, nam, item.IDTaiXe, item.SoXe);
                        if (listCapNhienLieu != null && listCapNhienLieu.Count > 0)
                            thongKeObj.SoDauTamUng = listCapNhienLieu.Sum(e => e.LuongNhienLieu);
                        var listNhienLieuTieuThu = ThongKeNhienLieuTieuThu.GetList(thang, nam, item.IDTaiXe, item.SoXe, TYPE_REPORT.BinhThuong);
                        if (listNhienLieuTieuThu != null && listNhienLieuTieuThu.Count > 0)
                            thongKeObj.SoDauTieuThu = listNhienLieuTieuThu.Sum(e => e.TongLit);
                        var donGiaObj = DonGiaNhienLieuBLL.GetDonGia(thang, nam, item.SoXe);
                        if (donGiaObj != null)
                            thongKeObj.DonGia = donGiaObj.DonGia;
                        thongKeObj.ThanhTien = thongKeObj.DonGia * thongKeObj.SoDauConLai;
                        listThongKe.Add(thongKeObj);
                    }
                    return listThongKe;
                }
                else
                    return null;
            }
        }
        public static List<ThongKeTongHopThanhToan> GetListByQuy(int quy, int nam)
        {
            using (var db = DBContextHelper.GetContext())
            {
                int thang = 0;
                switch (quy)
                {
                    case 1:
                        thang = 1;
                        break;
                    case 2:
                        thang = 4;
                        break;
                    case 3:
                        thang = 7;
                        break;
                    case 4:
                        thang = 10;
                        break;
                }
                List<ThongKeTongHopThanhToan> listThongKe = new List<ThongKeTongHopThanhToan>();
                var listQuyetToan1 = GetListByThang(thang, nam);
                if (listQuyetToan1 != null && listQuyetToan1.Count > 0)
                {
                    foreach (var item in listQuyetToan1)
                    {
                        var thongKeObj = new ThongKeTongHopThanhToan();
                        thongKeObj.IDTaiXe = item.IDTaiXe;
                        thongKeObj.TenDayDu = item.TenDayDu;
                        thongKeObj.SoXe = item.SoXe;
                        thongKeObj.SoDuDauKy = item.SoDuDauKy;
                        thongKeObj.TienMat = item.TienMat;
                        thongKeObj.SoDauTamUng = item.SoDauTamUng;
                        thongKeObj.SoDauTieuThu = item.SoDauTieuThu;
                        thongKeObj.ThanhTien = item.ThanhTien;
                        listThongKe.Add(thongKeObj);
                    }
                }
                var listQuyetToan2 = GetListByThang(thang + 1, nam);
                var listQuyetToan3 = GetListByThang(thang + 2, nam);
                if (listQuyetToan2 != null && listQuyetToan2.Count > 0)
                {
                    foreach (var item in listQuyetToan2)
                    {
                        var tk = listThongKe.Find(e => e.SoXe == item.SoXe && e.IDTaiXe == item.IDTaiXe);
                        if (tk != null)
                        {
                            tk.SoDauTamUng += item.SoDauTamUng;
                            tk.SoDauTieuThu += item.SoDauTieuThu;
                            tk.ThanhTien += item.ThanhTien;
                            tk.TienMat += item.TienMat;
                        }
                        else
                        {
                            var thongKeObj = new ThongKeTongHopThanhToan();
                            thongKeObj.IDTaiXe = item.IDTaiXe;
                            thongKeObj.TenDayDu = item.TenDayDu;
                            thongKeObj.SoXe = item.SoXe;
                            thongKeObj.SoDuDauKy = item.SoDuDauKy;
                            thongKeObj.TienMat = item.TienMat;
                            thongKeObj.SoDauTamUng = item.SoDauTamUng;
                            thongKeObj.SoDauTieuThu = item.SoDauTieuThu;
                            thongKeObj.ThanhTien = item.ThanhTien;
                            listThongKe.Add(thongKeObj);
                        }
                    }
                }
                if (listQuyetToan3 != null && listQuyetToan3.Count > 0)
                {
                    foreach (var item in listQuyetToan3)
                    {
                        var tk = listThongKe.Find(e => e.SoXe == item.SoXe && e.IDTaiXe == item.IDTaiXe);
                        if (tk != null)
                        {
                            tk.SoDauTamUng += item.SoDauTamUng;
                            tk.SoDauTieuThu += item.SoDauTieuThu;
                            tk.ThanhTien += item.ThanhTien;
                            tk.TienMat += item.TienMat;
                        }
                        else
                        {
                            var thongKeObj = new ThongKeTongHopThanhToan();
                            thongKeObj.IDTaiXe = item.IDTaiXe;
                            thongKeObj.TenDayDu = item.TenDayDu;
                            thongKeObj.SoXe = item.SoXe;
                            thongKeObj.SoDuDauKy = item.SoDuDauKy;
                            thongKeObj.TienMat = item.TienMat;
                            thongKeObj.SoDauTamUng = item.SoDauTamUng;
                            thongKeObj.SoDauTieuThu = item.SoDauTieuThu;
                            thongKeObj.ThanhTien = item.ThanhTien;
                            listThongKe.Add(thongKeObj);
                        }
                    }
                }
                return listThongKe;
            }
        }
    }
}
